#include "StdAfx.h"
#include "SDUData.h"
#include <string.h>

SDUData::SDUData(void)
{
	AVWData avwDataNew;
	this->time = 0;
	this->A = avwDataNew;
	this->B = avwDataNew;
	this->C = avwDataNew;
}

SDUData::~SDUData(void)
{
}

void SDUData::toBinaryDisplay(char* szSDUDataDisplay)
{
	char szTime[LONG_TYPE_SIZE+1] = "";
	char szA[AVWDATA_LENGTH+1] = "";
	char szB[AVWDATA_LENGTH+1] = "";
	char szC[AVWDATA_LENGTH+1] = "";

	szTime[LONG_TYPE_SIZE] = '\0';	
	szA[AVWDATA_LENGTH] = '\0';
	szB[AVWDATA_LENGTH] = '\0';
	szC[AVWDATA_LENGTH] = '\0';
	
	this->_parseUtil.longToBinary(this->time, szTime);
	this->A.toBinaryDisplay(szA);
	this->B.toBinaryDisplay(szB);
	this->C.toBinaryDisplay(szC);
	
	strcpy(szSDUDataDisplay, szTime);
	strcat(szSDUDataDisplay, szA);
	strcat(szSDUDataDisplay, szB);
	strcat(szSDUDataDisplay, szC);
	this->setFrame(szSDUDataDisplay);
}

void SDUData::getFrameField(int fieldNo, char* szFrameFieldString)
{
	int indexFieldSize[SDUDATA_FIELDS_COUNT] = {TIME_LENGTH,
												AVWDATA_LENGTH,
												AVWDATA_LENGTH,
												AVWDATA_LENGTH
												};
	int indexFieldStart[SDUDATA_FIELDS_COUNT] = {0,
												TIME_LENGTH,
												TIME_LENGTH+AVWDATA_LENGTH,
												TIME_LENGTH+2*AVWDATA_LENGTH,
												};
	_parseUtil.partitionFrameString(szFrameFieldString, indexFieldStart[fieldNo], indexFieldSize[fieldNo], this->szFrameData);
}

void SDUData::setFrame(char* szFrameString)
{
	strcpy(this->szFrameData, szFrameString);
}

void SDUData::setPropertiesFromFrameData()
{
	char szTmpTime[TIME_LENGTH+1] = "";
	char szTmpA[AVWDATA_LENGTH+1] = "";
	char szTmpB[AVWDATA_LENGTH+1] = "";
	char szTmpC[AVWDATA_LENGTH+1] = "";

	this->getFrameField(TIME_FIELD, szTmpTime);
	this->time = this->_parseUtil.binaryToLong(szTmpTime);

	AVWData avwDataTmpA;
	AVWData avwDataTmpB;
	AVWData avwDataTmpC;
	this->getFrameField(AVWDATA_A_FIELD, szTmpA);
	this->getFrameField(AVWDATA_B_FIELD, szTmpB);
	this->getFrameField(AVWDATA_C_FIELD, szTmpC);
	avwDataTmpA.setFrame(szTmpA);
	avwDataTmpB.setFrame(szTmpB);
	avwDataTmpC.setFrame(szTmpC);
	avwDataTmpA.setPropertiesFromFrameData();
	avwDataTmpB.setPropertiesFromFrameData();
	avwDataTmpC.setPropertiesFromFrameData();
	this->A = avwDataTmpA;
	this->B = avwDataTmpB;
	this->C = avwDataTmpC;
}
